﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using ProjManagSolution.pmMyWebService;

namespace ProjManagSolution
{
    public partial class EditProject : ChildWindow
    {
        
        protected int projectID;
        protected project currentProject = new project();

        public EditProject(int pid = 1)
        {
            projectID = pid;
            InitializeComponent();

            //get project info
            pmWebServiceClient ws = new pmWebServiceClient();
            ws.GetProjectDetailCompleted += new EventHandler<GetProjectDetailCompletedEventArgs>(ws_GetProjectDetailCompleted);
            ws.GetProjectDetailAsync(projectID);
        }

        void ws_GetProjectDetailCompleted(object sender, GetProjectDetailCompletedEventArgs e)
        {
            currentProject = e.Result;
            //bind info to form
            txtboxProjectName.Text = currentProject.project_name;
            txtboxDescription.Text = currentProject.project_description;
            txtblokStartDate.Text = currentProject.project_start_date.ToString();
        }

        protected bool Input_Validate()
        {
            if (txtboxProjectName.Text == "")
            {
                MessageBox.Show("Project name cannot be null!");
                return false;
            }

            return true;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            pmWebServiceClient ws = new pmWebServiceClient();
            ws.GetProjectsCompleted += new EventHandler<GetProjectsCompletedEventArgs>(ws_GetProjectsCompleted);
            ws.GetProjectsAsync();
        }

        void ws_GetProjectsCompleted(object sender, GetProjectsCompletedEventArgs e)
        {
            //check existing project
            List<project> rl = e.Result;
            bool ProjectNameExist = false;
            for (int i = 0; i < rl.Count; ++i)
            {
                if ((rl[i].project_name == txtboxProjectName.Text) 
                    && (rl[i].project_name != currentProject.project_name))
                {
                    ProjectNameExist = true;
                    break;
                }
            }

            if (ProjectNameExist)
            {
                MessageBox.Show("Project name exists!");
            }
            else
                if (Input_Validate())
                    this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

